<template>
  <div class="wrap">
    <div class="login">
      <div class="logo">
        <img src="../images/login/logo.png" alt>
      </div>
      <div class="title">亲子产业共创管理中心</div>
      <div class="input first">
        <input type="text" placeholder="请输入账号" v-model="userInfo.name">
      </div>
      <div class="input">
        <input type="password" placeholder="请输入密码" v-model="userInfo.pwd">
      </div>
      <div class="input">
        <input
          type="text"
          class="code"
          maxlength="6"
          @input="codeInp"
          placeholder="请输入验证码"
          v-model="code"
        >
        <div class="num" @click="getCode" style="cursor:pointer">{{recode}}</div>
      </div>
      <button class="input btn" @click="login()">登 录</button>
      <div class="info">忘记密码请联系统管理员进行重置</div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import qs from "qs";
export default {
  name: "login",
  data() {
    return {
      userInfo: {
        name: "",
        pwd: ""
      },
      code: "",
      recode: "",
      isEnter: true
    };
  },
  methods: {
    codeInp() {
      this.code = isNaN(this.code) ? 0 : this.code.replace(/\.|-/g, "");
    },
    /**
     * 登录
     */
    login: function() {
      let that = this;
      if (!this.userInfo.name) {
        that.isEnter = false;
        this.$alert("请输入账号", {
          confirmButtonText: "确定",
          callback: function() {
            that.isEnter = true;
          }
        });
      } else if (!this.userInfo.pwd) {
        that.isEnter = false;
        this.$alert("请输入密码", {
          confirmButtonText: "确定",
          callback: function() {
            that.isEnter = true;
          }
        });
      } else if (that.recode == that.code) {
        axios({
          method: "post",
          url: `${that.$store.state.url}/sysEmployee/login`,
          data: qs.stringify(that.userInfo)
        })
          .then(response => {
            let res = response.data;
            if (res.code == 0) {
              
              document.onkeydown = function() {
              };
              for (let i in res.data.employee) {
                if (i == "id") {
                  i = "pid";
                  this.$cookie.set(i, res.data.employee["id"]);
                } else {
                  this.$cookie.set(i, res.data.employee[i]);
                }
              }
              this.$cookie.set('loginTime',this.setDate(new Date()) + ' ' +this.setTime(new Date()))
              let logData = {
                loginTime:this.$cookie.get("loginTime"),
                account:this.$cookie.get("userAccount"),
                content:'登录',
              operationTime:this.setDate(new Date()) + ' ' +this.setTime(new Date())
              }
              axios({
          method: "post",
          url: `${that.$store.state.url}/log/addLog`,
          data: qs.stringify(logData)
        }).then( (res) => {
          console.log(res,'222')
        })
              // 登陆完同步状态
              if (this.$cookie.get("pid")) {
                this.$store.commit("partnerId", this.$cookie.get("pid"));
              }
              if (
                this.userInfo.pwd == "yunlehui2019" &&
                this.userInfo.name != "admin"
              ) {
                that.$router.push({
                  path: "/cpass"
                });
              } else {
                that.$router.push({
                  path: "/main"
                });
              }
            } else {
              that.isEnter = false;
              this.$alert(res.msg, {
                confirmButtonText: "确定",
                callback: function() {
                  that.isEnter = true;
                  that.getCode()
                }
              });
            }
          })
          .catch(res => {});
      } else {
        that.isEnter = false;
        this.$alert(this.code != "" ? "验证码错误!" : "请输入验证码", {
          confirmButtonText: "确定",
          callback: function() {
            that.isEnter = true;
            this.getCode();
          }
        });
        
      }
    },
    /**
     * 获取验证码 随机生成6位的验证码
     */
    getCode: function() {
      this.recode = "";
      for (var i = 0; i < 6; i++) {
        this.recode += Math.floor(Math.random() * 10);
      }
    },
    /**
     * 根据时间毫秒数 返回年月日
     */
    setDate: function(str) {
      let times = new Date(str);
      return `${times.getFullYear()}-${
        times.getMonth() + 1 < 10
          ? "0" + (times.getMonth() + 1)
          : times.getMonth() + 1
      }-${times.getDate() < 10 ? "0" + times.getDate() : times.getDate()}`;
    },
    /**
     * 根据时间毫秒数 返回时分秒
     */
    setTime: function(str) {
      let times = new Date(str);
      return `${
        times.getHours() < 10 ? "0" + times.getHours() : times.getHours()
      }:${
        times.getMinutes() < 10 ? "0" + times.getMinutes() : times.getMinutes()
      }:${
        times.getSeconds() < 10 ? "0" + times.getSeconds() : times.getSeconds()
      }`;
    }
  },
  mounted() {
    this.getCode();
  },
  created() {
    var that = this;
    document.onkeydown = function(e) {
      var key = window.event.keyCode;
      if (key == 13) {
        if (that.isEnter) {
          that.login();
        }
      }
    };
  }
};
</script>

<style scoped>
.wrap {
  width: 100%;
  height: 100vh;
  background: url("../images/login/bg.png");
}
.login {
  padding: 9px 27px;
  background: #ffffff;
  box-shadow: 0 2px 7px 0 rgba(0, 0, 0, 0.5);
  border-radius: 40px;
  width: 700px;
  height: 562px;
  box-sizing: border-box;
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  margin: auto;
  margin-top: 158px;
}
.logo {
  width: 140px;
  height: 54px;
}
.title {
  height: 50px;
  line-height: 50px;
  font-family: PingFangSC-Medium;
  font-size: 36px;
  color: #000;
  text-align: center;
  margin-top: 5px;
}
.input {
  box-shadow: 0 2px 4px 0 rgba(143, 143, 143, 0.5);
  border-radius: 100px;
  width: 338px;
  height: 49px;
  border: 1px solid #0e64e1;
  border-radius: 25px;
  margin: 0 auto;
  margin-top: 19px;
  color: #9b9b9b;
}
.first {
  margin-top: 50px;
}
input {
  width: 100%;
  border: 0;
  outline: none;
  height: 100%;
  background: none;
  text-indent: 2em;
  box-shadow: 0 0 0px 1000px transparent inset !important;
  -webkit-box-shadow: 0 0 0px 1000px transparent inset !important;
  background-color: transparent;
  background-image: none;
  transition: background-color 50000s ease-in-out 0s;
}
.num {
  font-family: PingFangSC-Medium;
  font-size: 14px;
  color: #333333;
  float: right;
  border-left: 1px solid #0e64e1;
  width: 80px;
  height: 50px;
  line-height: 50px;
  text-align: center;
  letter-spacing: 1.54px;
}
.code {
  width: calc(100% - 100px);
  float: left;
}
.btn {
  margin: 0 auto;
  margin-top: 45px;
  background: #0e64e1;
  color: #fff;
  box-shadow: 0 0 8px #bcbcbc;
  outline: none;
  cursor: pointer;
  font-family: PingFangSC-Medium;
  display: block;
}
.info {
  margin-top: 34px;
  line-height: 20px;
  text-align: center;
  font-family: PingFangSC-Medium;
  font-size: 14px;
  color: #9b9b9b;
  height: 20px;
}
</style>
